<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/25
 * Time: 15:42
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\Protocol as ProtocolModel;
use think\Request;

class Protocol extends Base
{
    protected $beforeActionList = [
        'isAdmin'=>['only'=>'index,protocollist,protocolpage,updateprotocol'],
        'operationSelect'=>['only'=>'index,protocollist,protocolpage'],
        'operationUpdate'=>['only'=>'updateprotocol']
    ];
    public function index()
    {
        return view('index');
    }
    public function protocolList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        return ProtocolModel::protocolList($page,$rows,$sort,$sortOrder);
    }
    //编辑协议页面
    public function protocolPage()
    {
        $id = Request::instance()->only('id');
        $data = ProtocolModel::where('id',$id['id'])->find();
        return view('update',compact('data'));
    }
    //修改协议内容
    public function updateProtocol()
    {
        $data = Request::instance()->only('id,title,text');
        $text = htmlspecialchars($data['text']);
        $is_update = ProtocolModel::where('id',$data['id'])->update(["title"=>$data['title'],"text"=>$text]);
        if(!$is_update){
            return json(['msg'=>'修改失败', 'errorCode'=>203]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
}